// pages/business/myagentinfo/myagentinfo.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    start_date: '',
    end_date: '',
    start_min_date: '',
    start_max_date: '',
    end_min_date: '',
    end_max_date: '',
    currentReportIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { //
    /**
     * options参数  index 1-代理商查看资料  2-查看下级代理商资料  3-联系客服
     */
    var that = this,
      title = '',
      method = 'POST',
      url = '',
      data = {};
    that.setData({
      currentIndex: options.index
    })
    if (options.index == 1) {
      title = '查看资料';
      wx.getStorage({
        key: 'mineagentinfo',
        success: function (res) {//查看我的资料
          that.setData({
            detail: res.data
          })
        }
      })
    } else if (options.index == 2) {//查看下级代理商资料
      title = '查看详情';
      data["id"] = options.id;
      url = config.agent_child_detail; 
    } else if (options.index == 3) {//联系客服
      method = 'GET';
      title = '联系客服';
      url = config.get_online_contact; 
    } else if (options.index == 4) {//统计
      data["id"] = options.id;
      url = config.agent_report_agent;
      var day = new Date();
      day.setTime(day.getTime() - 24 * 60 * 60 * 1000);
      var yesterday = day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate();
      that.setData({
        end_date: yesterday,
        report_id: options.id
      })
    }
    wx.setNavigationBarTitle({
      title: title,
    })
    shihan.sendRequest(url, method, data)
      .then(function(response) {
        if (that.data.currentIndex==4) {
          wx.setNavigationBarTitle({
            title: response.data.data.sh_title
          })
          // if (response.data.data.sh_month_arr.length>0){
          //   response.data.data.sh_month_arr = response.data.data.sh_month_arr.concat(response.data.data.sh_month_arr);
          // }
          // that.setData({
          //   start_min_date: response.data.data.sh_min_date,
          //   start_max_date: response.data.data.sh_max_date,
          //   end_min_date: '2018-09-01',
          //   end_max_date: '2019-09-10',
          //   end_max_date: response.data.data.sh_max_date
          // })
        }
        that.setData({
          detail: response.data.data,
          start_date: response.data.data.sh_start_date,
          end_date: response.data.data.sh_end_date,
        })
      }, function(error) {
        console.log(error);
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  copyContent: function(e) {
    var that = this
    wx.setClipboardData({
      data: String(e.currentTarget.dataset.content),
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            if (res.data) {
              console.log('复制成功');
            }
          }
        })
      }
    })
  },
  phoneCall: function(e) {
    var that = this
    wx.makePhoneCall({
      phoneNumber: new String(e.currentTarget.dataset.mobile)
    })
  },

  changeReport: function (e) {
    this.setData({
      currentReportIndex: e.currentTarget.dataset.index
    })
  },
  bindDateChange: function (e) {
    this.setData({
      start_date: e.detail.value,
      end_min_date: e.detail.value
    })
  },
  bindYesDateChange: function (e) {
    this.setData({
      end_date: e.detail.value,
      start_max_date: e.detail.value
    })
  },
  exportReport: function () {
    var day = Number(this.DateMinus(this.data.start_date, this.data.end_date));
    if (day < 0) {
      shihan.showToast.call(this, '请选择合理的时间区间');
    } else if (day > Number(this.data.detail.sh_export_day)) {
      shihan.showToast.call(this, this.data.detail.sh_export_day_txt);
    } else {
      wx.navigateTo({
        url: '/pages/business/sweepcode/sweepcode?index=5' + '&id=' + this.data.report_id + '&name=' + this.data.detail.sh_agent_name + '&sdate=' + this.data.start_date + '&ydate=' + this.data.end_date,
      })
    }
  },
  serachReport: function () {
    var that = this, data = {}, YearDays = that.getYearDays(), day = Number(this.DateMinus(this.data.start_date, this.data.end_date));
    data["id"] = that.data.report_id;
    data["start_date"] = that.data.start_date;
    data["end_date"] = that.data.end_date;
    that.setData({ showLoading: true, currentReportIndex: 0 })
    console.log(YearDays)
    console.log(day)
    shihan.sendRequest(config.agent_report_agent, 'POST', data)
      .then(function (response) {
        // that.data.detail.sh_month_arr = response.data.data.sh_month_arr
        // that.setData({
        //   detail: that.data.detail
        // })
        if (response.data.code==0) {
          that.setData({
            detail: response.data.data
          })
        } else {
          shihan.showToast.call(that, response.data.msg);
        }
        that.setData({
          showLoading: false
        })
      }, function (error) {
        console.log(error);
      });
  },
  DateMinus: function (sDate, yDate){
    var sdate = new Date(sDate.replace(/-/g, "/"));
    var ydate = new Date(yDate.replace(/-/g, "/"));
    var days = ydate.getTime() - sdate.getTime();
    var day = parseInt(days / (1000 * 60 * 60 * 24));
    return day;
  },
  getYearDays: function () {
    var y = new Date().getFullYear(),
      isLeap = (0 === y % 4) && (0 === y % 100) || (0 === y % 400),
      days = isLeap ? 366 : 365;
    return days;
  },
  openTrade: function () {
    if (this.data.report_id) {//代理商统计
      wx.navigateTo({
        url: '/pages/business/agenttrade/agenttrade',
      })
    } else {//我的统计
      wx.navigateTo({
        url: '/pages/business/myorder/myorder?index=0',
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})